<?php
mysql_connect('localhost', 'root');
mysql_select_db('lojavirtual');

function query($query, $fields = false) {
  
  if ($fields) {
    $fetch_row = 'mysql_fetch_field';
  } else {
    $fetch_row = 'mysql_fetch_assoc';
  }
  
  $oResult = array();
  
  $oRes = mysql_query($query);
  
  while ($dados = $fetch_row($oRes)) {
    $oResult[] = (array) $dados;
  }
  
  return $oResult;
}

function epr($var) {
  echo '<pre>';
  print_r($var);
  echo '</pre>';
}

function cria_class($table, $fields) {
  $class = ucfirst($table);
  $file_content = '';
  $file_content .= '<?php'. PHP_EOL;
  $file_content .= 'class '.$class.' {'.PHP_EOL;
  foreach ($fields as $i => $campo) {
    $file_content .= '  private $'.$campo.';'.PHP_EOL;
  }
  $file_content .= '  '.PHP_EOL;

  $file_content .= '  public function __construct($sFiltro = "", $sOrdem = "'.$fields[0].'") {'.PHP_EOL;
  //$file_content .= '    '.PHP_EOL;
  $file_content .= '    global $oDB;'.PHP_EOL;
  $file_content .= '    $sSQL = "SELECT '.implode(', ', $fields).' FROM '.$table.'";'.PHP_EOL;
  $file_content .= '    if ($sFiltro) {'.PHP_EOL;
  $file_content .= '      $sSQL .= " WHERE ($sFiltro)";'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '    if ($sOrdem) {'.PHP_EOL;
  $file_content .= '      $sSQL .= " ORDER BY $sOrdem";'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '    if (!empty($sFiltro)) {'.PHP_EOL;
  $file_content .= '      $oRes = $oDB->Execute($sSQL);'.PHP_EOL;
  $file_content .= '      if ($oRes == false) {'.PHP_EOL;
  $file_content .= '        trigger_error($oDB->ErrorMsg());'.PHP_EOL;
  $file_content .= '      } else {'.PHP_EOL;
  foreach ($fields as $field) {
    $file_content .= '        $this->'.$field.' = $oRes->fields[\''.$field.'\'];'.PHP_EOL;
  }
  $file_content .= '      }'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '  }'.PHP_EOL;
  $file_content .= '  '.PHP_EOL;


  // gets e sets
  foreach ($fields as $campo) {
    $fcampo = ucfirst($campo);
    $file_content .= '  public function get'.$fcampo.'() {'.PHP_EOL;
    $file_content .= '    return $this->'.$campo.';'.PHP_EOL;
    $file_content .= '  }'.PHP_EOL;
    $file_content .= '  public function set'.$fcampo.'($valor) {'.PHP_EOL;
    $file_content .= '    $this->'.$campo.' = $valor;'.PHP_EOL;
    $file_content .= '  }'.PHP_EOL;
    $file_content .= '  '.PHP_EOL;
  }
  
  $file_content .= '  public function atualizar() {'.PHP_EOL;
  $file_content .= '    global $oDB;'.PHP_EOL;
  $file_content .= '    $campos = array('.PHP_EOL;
  for ($i = 1; $i < count($fields); $i++) {
    $file_content .= '      \''.$fields[$i].'\' => $this->'.$fields[$i].','.PHP_EOL;
  }
  $file_content .= '    );'.PHP_EOL;
  $file_content .= '    if ($this->'.$fields[0].' > 0) {'.PHP_EOL;
  $file_content .= '      $oDB->AutoExecute(\''.$table.'\', $campos, \'UPDATE\', \''.$fields[0].' = \'.intval($this->'.$fields[0].'));'.PHP_EOL;
  $file_content .= '    } else {'.PHP_EOL;
  $file_content .= '      $oDB->AutoExecute(\''.$table.'\', $campos, \'INSERT\');'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '  }'.PHP_EOL;

  $file_content .= '  public function excluir($aIds) {'.PHP_EOL;
  $file_content .= '    global $oDB;'.PHP_EOL;
  $file_content .= '    $sIds = implode(\',\', $aIds);'.PHP_EOL;

  $file_content .= '    $sSQL = \'DELETE FROM '. $table .' WHERE '. $fields[0] .' IN (\'. $sIds .\')\';'.PHP_EOL;
  $file_content .= '    $oRes = $oDB->execute($sSQL);'.PHP_EOL;

  $file_content .= '    if ($oRes != false) {'.PHP_EOL;
  $file_content .= '      return true;'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '  }'.PHP_EOL;
    
  $file_content .= '  public static function listar($filtro = "", $sOrdem = "'.$fields[0].'") {'.PHP_EOL;
  //$file_content .= '    '.PHP_EOL;
  $file_content .= '    global $oDB;'.PHP_EOL;
  $file_content .= '    $lista = array();'.PHP_EOL;
  $file_content .= '    $sSQL = "SELECT '.implode(', ', $fields).' FROM '.$table.'";'.PHP_EOL;
  $file_content .= '    if ($filtro) {'.PHP_EOL;
  $file_content .= '      $sSQL .= " WHERE ($filtro)";'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '    if ($sOrdem) {'.PHP_EOL;
  $file_content .= '      $sSQL .= " ORDER BY $sOrdem";'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '    $oRes = $oDB->Execute($sSQL);'.PHP_EOL;
  $file_content .= '    if ($oRes == false) {'.PHP_EOL;
  $file_content .= '      trigger_error($oDB->ErrorMsg());'.PHP_EOL;
  $file_content .= '    } else {'.PHP_EOL;
  $file_content .= '      while (!$oRes->EOF) {'.PHP_EOL;
  $file_content .= '        $linha = new self();'.PHP_EOL;
  foreach ($fields as $field) {
    $file_content .= '        $linha->'.$field.' = $oRes->fields[\''.$field.'\'];'.PHP_EOL;
  }
  $file_content .= '        $lista[] = $linha;'.PHP_EOL;
  $file_content .= '        '.PHP_EOL;
  $file_content .= '        $oRes->MoveNext();'.PHP_EOL;
  $file_content .= '      }'.PHP_EOL;
  $file_content .= '    }'.PHP_EOL;
  $file_content .= '    return $lista;'.PHP_EOL;
  $file_content .= '  }'.PHP_EOL;
  $file_content .= '  '.PHP_EOL;  
  
  $file_content .= '}'.PHP_EOL;
  $file_content .= '?>'. PHP_EOL;
  echo highlight_string($file_content, true);
  file_put_contents('classes/class.'.$class.'.php', $file_content);
}

$tables = query('show tables');
$classes = array();
foreach ($tables as $table) {
  $table = array_values($table);
  $table = $table[0];
  $campos = query('select * from '.$table, 1);
  //echo '<br />'.$table.'<br />';
  foreach ($campos as $campo) {
    //echo htmlentities('>').$campo['name'].'<br />';
    //epr($campo);
    $classes[$table][] = $campo['name'];
  }
}
//epr($classes);
foreach ($classes as $class => $fields) {
  cria_class($class, $fields);
}